<?php

/**
 * @version         $Id$
 * @create          2012-04-21 11:04:10 By xjiujiu
 * @description     HongJuZi Framework
 * @copyRight       Copyright (c) 2011-2012 http://www.xjiujiu.com.All right reserved
 */
defined('_HEXEC') or die('Restricted access!');

//导入模块工具类
HClass::import('config.popo.actorpopo, app.admin.action.AdminAction, model.actormodel');

/**
 * 用户角色的动作类 
 * 
 * 主要处理后台管理主页的相关请求动作 
 * 
 * @author          xjiujiu <xjiujiu@foxmail.com>
 * @package         app.admin.action
 * @since           1.0.0
 */
class ActorAction extends AdminAction
{

    /**
     * 构造函数 
     * 
     * 初始化类变量 
     * 
     * @access public
     */
    public function __construct() 
    {
        parent::__construct();
        $this->_popo        = new ActorPopo();
        $this->_model       = new ActorModel($this->_popo);
        $this->_listTpl     = 'actor/list';
        $this->_model->setMustWhere('for_system', '`for_system` = 1');
    }

    protected function _otherJobsAfterInfo()
    {
        parent::_otherJobsAfterInfo();
        $record     = HResponse::getAttribute('record');
        $this->_assignRightsList($record);
        $this->_assignLeftSideBarMenuList($record);
        $this->_assignModelManagerList($record);
    }

    private function _assignModelManagerList($record)
    {
        $modelManager   = HClass::quickLoadModel('modelmanager');
        $list           = $modelManager->getAllRows('`support_admin` = 2');
        $this->_linkedData      = HClass::quickLoadModel('linkeddata');
        $this->_linkedData->setRelItemModel('actor', 'modelmanager');
        $rList      = $this->_linkedData->getAllRowsByFields(
            '`item_id`', 'rel_id = ' . $record['id']
        );
        $ids        = array();
        foreach($rList as $item) {
            $ids[]  = $item['item_id'];
        }
        HResponse::setAttribute(
            'modelManagerNodes', 
            HString::formatToZTreeNodes($list, $ids)
        );
    }

    private function _assignLeftSideBarMenuList($record)
    {
        $adminNavmenu   = HClass::quickLoadModel('adminnavmenu');
        $list           = $adminNavmenu->getAllRows('`support_admin` = 2');
        $this->_linkedData      = HClass::quickLoadModel('linkeddata');
        $this->_linkedData->setRelItemModel('actor', 'leftmenu');
        $rList      = $this->_linkedData->getAllRowsByFields(
            '`item_id`', 'rel_id = ' . $record['id']
        );
        $ids        = array();
        foreach($rList as $item) {
            $ids[]  = $item['item_id'];
        }
        HResponse::setAttribute(
            'leftSidebarMenuNodes', 
            HString::formatToZTreeNodes($list, $ids)
        );
    }

    /**
     * {@inheritDoc}
     */
    protected function _assignRightsList($record)
    {
        $rights     = HClass::quickLoadModel('rights');
        $list       = $rights->getAllRows('1 = 1');
        $this->_linkedData      = HClass::quickLoadModel('linkeddata');
        $this->_linkedData->setRelItemModel('actor', 'rights');
        $rList      = $this->_linkedData->getAllRowsByFields(
            '`item_id`', 'rel_id = ' . $record['id']
        );
        $ids        = array();
        foreach($rList as $item) {
            $ids[]  = $item['item_id'];
        }
        HResponse::setAttribute(
            'rightsNodes',
            HString::formatToZTreeNodes($list, $ids)
        );
    }

    public function addview()
    {
        throw new HVerifyException('功能不支持！');
    }

    public function add()
    {
        throw new HVerifyException('功能不支持！');
    }

    public function delete()
    {
        throw new HVerifyException('不支持删除！');
    }

    /**
     * 重写编辑
     */
    public function edit()
    {
        $id                     = HVerify::isEmptyByVal(HRequest::getParameter('id'), '角色编号');
        $this->_linkedData      = HClass::quickLoadModel('linkeddata');
        $this->_addRightsLinkedData($id);
        $this->_addLeftMenuLinkedData($id);
        $this->_addModelManagerLinkedData($id);

        HResponse::json(array('rs' => true));
    }

    /**
     * 加入权限关联数据
     * 
     * @author xjiujiu <xjiujiu@foxmail.com>
     * @access private
     */
    private function _addRightsLinkedData($id)
    {
        $ids      = array_filter(explode(',', HRequest::getParameter('rights_ids')));
        if(!$ids) {
            return;
        }
        $list           = array();
        $this->_linkedData->setRelItemModel('actor', 'rights');
        $this->_linkedData->deleteByWhere(
            '`rel_id` = ' . $id
        );
        foreach($ids as $itemId) {
            $list[]     = array(
                'uniacid' => HSession::getAttribute('acid', 'curWeiXinInfo'),
                'item_id' => $itemId,
                'rel_id' => $id,
                'author' => HSession::getAttribute('id', 'user')
            );
        }
        $this->_linkedData->addMore(array_keys($list[0]), $list);
    }

    /**
     * 加入右菜单关联数据
     * 
     * @author xjiujiu <xjiujiu@foxmail.com>
     * @access private
     */
    private function _addLeftMenuLinkedData($id)
    {
        $ids      = array_filter(explode(',', HRequest::getParameter('leftmenu_ids')));
        if(!$ids) {
            return;
        }
        $list           = array();
        $this->_linkedData->setRelItemModel('actor', 'leftmenu');
        $this->_linkedData->deleteByWhere('`rel_id` = ' . $id);
        foreach($ids as $itemId) {
            $list[]     = array(
                'uniacid' => HSession::getAttribute('acid', 'curWeiXinInfo'),
                'item_id' => $itemId,
                'rel_id' => $id,
                'author' => HSession::getAttribute('id', 'user')
            );
        }
        $this->_linkedData->addMore('`uniacid`, `item_id`, `rel_id`, `author`', $list);
    }

    /**
     * 加入模块关联数据
     * 
     * @author xjiujiu <xjiujiu@foxmail.com>
     * @access private
     */
    private function _addModelManagerLinkedData($id)
    {
        $ids      = array_filter(explode(',', HRequest::getParameter('modelmanager_ids')));
        if(!$ids) {
            return;
        }
        $list           = array();
        $this->_linkedData->setRelItemModel('actor', 'modelmanager');
        $this->_linkedData->deleteByWhere('`rel_id` = ' . $id);
        foreach($ids as $itemId) {
            $list[]     = array(
                'uniacid' => HSession::getAttribute('acid', 'curWeiXinInfo'),
                'item_id' => $itemId,
                'rel_id' => $id,
                'author' => HSession::getAttribute('id', 'user')
            );
        }
        $this->_linkedData->addMore('`uniacid`, `item_id`, `rel_id`, `author`', $list);
    }

}

?>
